package com.distribute.framework.plugin;


import java.lang.reflect.Field;
import com.distribute.framework.core.lang.database;
import com.distribute.framework.core.sql.redis.RedisSupport;
import com.distribute.framework.core.util.Function;
import com.distribute.framework.config.DatabaseNames;


public class Cache {

	public static final String prefix = "DISTRIBUTE";
	
	public static final String defaultRedisFlag = "redis.framework";
	
	public static RedisSupport defaultRedisSupport = RedisSupport.getRedis(defaultRedisFlag);
	
	public static Boolean refush = RedisSupport.getRedis(defaultRedisFlag).getValue("INI", "refush")!=null;
	
	public static Integer defaultSeconds(){
		return 7200;
	}
	
	public static String getClassName(Class modelClass){
		return prefix+"."+(modelClass.getName().replaceAll("[^\\.]*\\.", ""));
	};
	
	private static String getRedisFlag(Class modelClass) {
		database database = (database) modelClass.getAnnotation(database.class);
		if(database==null)return defaultRedisFlag;
		try {
			Field field = DatabaseNames.class.getDeclaredField(database.value()+"_redis");
			if(field==null)return defaultRedisFlag;
			field.setAccessible(true);
			return (String) field.get(null);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultRedisFlag;
		}
	}
	
	
	/**
	 * 缓存查询，有值直接返回值，没值就执行查询
	 * @param <T>
	 * @param key
	 * @param function
	 * @param seconds
	 * @return
	 */
	public static <T> T cache(String key,Function function,Integer seconds){
		T retval = RedisSupport.getRedis(defaultRedisFlag).getObject("FUNC", ""+key);
		if(retval == null){
			retval = (T) RedisSupport.getRedis(defaultRedisFlag).setObject("FUNC", ""+key, function.execute(), seconds);
		}
		return retval;
	}
	
	/**
	 * 对象缓存
	 * @param string
	 * @param spId
	 * @param keyValue
	 */
	public static Object setObject(Class modelClass, Object key, Object value) {
		RedisSupport.getRedis(getRedisFlag(modelClass)).setObject(getClassName(modelClass),""+key,value);
		return value;
	}
	public static Object setObject(Class modelClass, Object key, Object value,int seconds) {
		RedisSupport.getRedis(getRedisFlag(modelClass)).setObject(getClassName(modelClass),""+key,value,seconds);
		return value;
	}
	public static <T> T getObject(Class modelClass, Object key) {		
		return RedisSupport.getRedis(getRedisFlag(modelClass)).getObject(getClassName(modelClass),""+key);
	}
	public static Long deleteObject(Class modelClass, Object key) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).deleteObject(getClassName(modelClass),""+key);
	}
	public static Long clearObject(Class modelClass) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).clearObject(getClassName(modelClass));
	}
	
	
	/**
	 * 键值缓存
	 * @param string
	 * @param spId
	 * @param keyValue
	 */
	public static String setValue(Class modelClass, Object key, String value) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).setValue(getClassName(modelClass),""+key,value);
	}
	public static String setValue(Class modelClass, Object key, String value,int seconds) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).setValue(getClassName(modelClass),""+key,value,seconds);
	}
	public static String getValue(Class modelClass, Object key) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).getValue(getClassName(modelClass),""+key);
	}
	public static Long deleteValue(Class modelClass, Object key) {		
		return RedisSupport.getRedis(getRedisFlag(modelClass)).deleteValue(getClassName(modelClass),""+key);
	}
	public static Long clearValue(Class modelClass) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).clearValue(getClassName(modelClass));
	}
	public static String kv(Class modelClass, Object key, String value) {
		return RedisSupport.getRedis(getRedisFlag(modelClass)).setValue(getClassName(modelClass),""+key,value);
	}
	public static String kv(Class modelClass, Object key) {		
		return RedisSupport.getRedis(getRedisFlag(modelClass)).getValue(getClassName(modelClass),""+key);
	}
	
	
	public static String setValue(Object key, String value) {
		return RedisSupport.getRedis(defaultRedisFlag).setValue(prefix+"."+"Def",""+key,value);
	}
	public static String getValue(Object key) {
		return RedisSupport.getRedis(defaultRedisFlag).getValue(prefix+"."+"Def",""+key);
	}
	public static String setValue(Object key, String value,int seconds) {
		return RedisSupport.getRedis(defaultRedisFlag).setValue(prefix+"."+"Def",""+key,value,seconds);
	}
	
	
}